import {
    Injectable,
    NestInterceptor,
    ExecutionContext,
    CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface Response<T> {
    data: T;
    success: boolean;
    message?: string;
}

@Injectable()
export class TransformInterceptor<T>
    implements NestInterceptor<T, Response<T>> {
    intercept(
        context: ExecutionContext,
        next: CallHandler,
    ): Observable<Response<T>> {
        return next.handle().pipe(
            map(data => {
                // 如果已经是标准响应格式，直接返回
                if (data && typeof data === 'object' && 'success' in data) {
                    return data;
                }
                // 否则包装成标准格式
                return {
                    data,
                    success: true,
                };
            }),
        );
    }
} 